package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.vo.CheckGroupVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("ALL")
/**
 * @Description :
 * @author     :LD
 */
@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {
    @Autowired
    private CheckItemService checkItemService;

    @Override
    public Map findPage(QueryPageBean queryPageBean) {
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        //根据name模糊查询
        String param = queryPageBean.getQueryString();
        queryWrapper.eq("is_delete",0);
        queryWrapper.and(i->i.like("name",param==null?"":param).or().like("helpcode",param==null?"":param));
        Page<CheckGroup> page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);
        List<CheckGroup> records = page.getRecords();
        long total = page.getTotal();
        Map map=new HashMap();
        map.put("list",records);
        map.put("total",total);
        return map;
    }

    @Override
    public void add(CheckGroupDTO checkGroupDTO) {
        //添加检查组(注意此处默认主键回填)
        save(checkGroupDTO);
        //添加数据到检查组检查项的中间表中
        Integer id = checkGroupDTO.getId();
        for (Integer itemId : checkGroupDTO.getCheckitemIds()) {
            baseMapper.addCheckGroupAndCheckItemIds(id,itemId);
        }
    }

    @Override
    public CheckGroupVO findItemsAndIds(Integer id) {
        CheckGroupVO checkGroupVO = new CheckGroupVO();
        //查询所有的检查项
        List<CheckItem> all = checkItemService.findAll();
        checkGroupVO.setCheckItems(all);
        //根据组id查询检查项
        int[] ids = baseMapper.findItemByGroupId(id);
        checkGroupVO.setCheckItemIds(ids);
        return checkGroupVO;
    }

    @Override
    public void edit(CheckGroupDTO checkGroupDTO) {
        Integer[] checkitemIds = checkGroupDTO.getCheckitemIds();
        Integer id = checkGroupDTO.getId();
        //根据组id删除中间表
        if (checkitemIds.length!=0){
            baseMapper.deleteByGroupId(id);
        }
        //更新检查组
        updateById(checkGroupDTO);
        //添加数据到检查组检查项的中间表中
        for (Integer itemId : checkitemIds) {
            baseMapper.addCheckGroupAndCheckItemIds(id,itemId);
        }
    }

    @Override
    public List<CheckGroup> findAll() {
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",0);
        return list(queryWrapper);
    }

}
